/**
 * Created by Mark_ren on 2017/5/31.
 * 苟利国家生死以，岂因祸福避趋之
 */
var uid = getQueryString("uid");
var item = getQueryString("item");
var sort = getQueryString("sort");
$(function () {


    var typeSel = $.Params.create();
    typeSel.ajax(getIP() + "api/v2.0/admin/dictionarys/" + sort, function (data) {
        if (data.success) {
            var typeHtml = "<option value = 0>请选择分类</option>";
            $.each(data.result, function (i, val) {
                typeHtml += "<option value = '" + val.codeId + "'>" + val.codeDesc + "</option>";
            })
            $("#types").html(typeHtml);
        } else {
            alert(data.msg);
        }
    })
    getData();
    //获取数据
    function getData() {
        var sel = $.Params.create();
        sel.addParams("id", uid);
        sel.ajax(getIP() + "api/v2.0/admin/articles/JW/", function (data) {
            if (data.success) {
                var html = "";
                if (data.result[0] != null) {

                    $("#items").val(item);
                    $("#sorts").val(sort);
                    $("#types").find("option[value=" + data.result[0].type + "]").attr("selected", "selected");
                    $("#pUrl").attr("src", getIP() + "data/articleCover/" + data.result[0].titlePic);
                    $("#title").val(data.result[0].title);
                    $("#addressUrl").val(data.result[0].URL);

                }
                $("#listContainer").html(html);
            }
        })
    }

    $("#loadImg").on("change", function (e) {
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
        // for (var i = 0, len = files.length; i < len; ++i) {
        var file = files[0];
        if (url) {
            src = url.createObjectURL(file);
        } else {
            src = e.target.result[0];
        }

        $("#pUrl").attr('src', src);
        // }
    });
    //取消时
    $("#cancelAdd").on("click", function () {
        window.history.go(-1);
    })
});



$("#updateData").on("click", function () {
    $("#updateForm").attr("action", getIP() + "api/v2.0/admin/articles/" + uid);
    $("#updateForm").submit();
})
